package fge;

import com.l2fprod.common.propertysheet.Property;
import com.l2fprod.common.propertysheet.PropertySheet;
import com.l2fprod.common.propertysheet.PropertySheetPanel;

import java.beans.*;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyChangeEvent;


public class propPanel extends PropertySheetPanel
{
	private Object bean = null;
	private Main editor;	// To update the graph instantly, we call editor.graph.repaint();

	public propPanel(Main _editor)
	{
		this.editor = _editor;
		this.setMode(PropertySheet.VIEW_AS_CATEGORIES);
		this.setToolBarVisible(true);
		this.setDescriptionVisible(true);
		this.setToolBarVisible(true);
	}

	/**
	 * This is called whenever the user has updated a property value of some object
	 */
	@Override
	public void propertyChange(PropertyChangeEvent evt)
	{
		// Write the new value to the object
		Property prop = (Property) evt.getSource();
		prop.writeToObject(propPanel.this.bean);

		repaint();	// Update the GUI property-editor widget instantly

		// Update the graph instantly
		editor.graph.repaint();
		editor.graph.refresh();
		editor.graphComponent.refresh();
		editor.graphComponent.repaint();
		editor.treeview.update();
	}

	/**
	 * Clears the property table
	 */
	public void clear()
	{
		PropertyDescriptor[] nothing = new PropertyDescriptor[0];
		this.setProperties(nothing);
	}

	public void setBean(Object bean)
	{
		this.bean = bean;
		BeanInfo bi = null;

		try {
			bi = Introspector.getBeanInfo(bean.getClass());
		} catch (Exception e) {
			this.clear();
			return;					// Skip rest; don't even try to update
		}
		
		this.setBeanInfo(bi);		// Populate the key-part of properties
		this.readFromObject(bean);	// Populate the value-part of properties
	}

	public Object getBean() {
		return bean;
	}
}
